package com.woniuxy.cglibproxy;

import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author: 马宇航
 * @Todo: CgLib动态代理，需要额外jar包 spring-aop,它支持没有接口的类！
 * @DateTime: 22/03/28/0028 上午 11:43
 * @Component: 成都蜗牛学苑
 **/
public class CglibProxyFactory {
    public Object createProxyObject(Object obj){
        Object o = Enhancer.create(obj.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                //前置方法
                System.out.println("怪兽cglib入侵地球！");
                //这种写法不用去记忆
                Object o1 = methodProxy.invokeSuper(o, objects);
                //后置方法
                System.out.println("cglib怪兽被打败了！");
                return o1;
            }
        });
        return o;
    }
}
